package com.tjec.project.projectLeaderWorkbeanch.service.impl;

import java.util.*;

import com.tjec.common.constant.Constants;
import com.tjec.common.enums.NumCode;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.project.mdmSynchronous.api.JSONUtils;
import com.tjec.project.mdmSynchronous.api.ThreadPost;
import com.tjec.project.mdmSynchronous.service.MdmService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.domain.BmsProjectMember;
import com.tjec.project.projectWorkbench.service.IBmsProjectMemberService;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.projectLeaderWorkbeanch.mapper.BmsLeaderMemberMapper;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 项目负责人人员Service业务层处理
 * 
 * @author tjec
 * @date 2020-04-14
 */
@Service
public class BmsLeaderMemberServiceImpl implements IBmsLeaderMemberService 
{
    @Autowired
    private BmsLeaderMemberMapper bmsLeaderMemberMapper;

    @Autowired
    private IBmsProjectMemberService bmsProjectMemberService;

    @Autowired
    private IBmsProjectService bmsProjectService;

    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询项目负责人人员
     * 
     * @param guid 项目负责人人员ID
     * @return 项目负责人人员
     */
    @Override
    public BmsLeaderMember selectBmsLeaderMemberByGuid(String guid)
    {
        return bmsLeaderMemberMapper.selectBmsLeaderMemberByGuid(guid);
    }

    @Override
    public List<BmsLeaderMember> selectBmsLeaderMemberByGuids(String[] guids) {
        return bmsLeaderMemberMapper.selectBmsLeaderMemberByGuids(guids);
    }

    /**
     * 查询项目负责人人员列表
     * 
     * @param bmsLeaderMember 项目负责人人员
     * @return 项目负责人人员
     */
    @Override
    public List<BmsLeaderMember> selectBmsLeaderMemberList(BmsLeaderMember bmsLeaderMember)
    {
        return bmsLeaderMemberMapper.selectBmsLeaderMemberList(bmsLeaderMember);
    }

    /**
     * 新增项目负责人人员
     * 
     * @param bmsLeaderMember 项目负责人人员
     * @return 结果
     */
    @Override
    public int insertBmsLeaderMember(BmsLeaderMember bmsLeaderMember)
    {
        //默认设置为0=成员
        if(StringUtils.isBlank(bmsLeaderMember.getRole())){
            bmsLeaderMember.setRole("0");
        }
        bmsLeaderMember.setCreateTime(DateUtils.getNowDate());
        bmsLeaderMember.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        if(null==bmsLeaderMember.getGuid()||("").equals(bmsLeaderMember.getGuid())){
            bmsLeaderMember.setGuid(UUID.randomUUID().toString());
        }
        return bmsLeaderMemberMapper.insertBmsLeaderMember(bmsLeaderMember);
    }

    /**
     * 修改项目负责人人员
     * 
     * @param bmsLeaderMember 项目负责人人员
     * @return 结果
     */
    @Override
    public int updateBmsLeaderMember(BmsLeaderMember bmsLeaderMember)
    {
        bmsLeaderMember.setUpdateTime(DateUtils.getNowDate());
        bmsLeaderMember.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        return bmsLeaderMemberMapper.updateBmsLeaderMember(bmsLeaderMember);
    }
    
    /**
     * 修改项目负责人人员
     * 
     * @param bmsLeaderMember 项目负责人人员
     * @return 结果
     */
    @Override
    public int updateAgencyFlag(BmsLeaderMember bmsLeaderMember)
    {
        bmsLeaderMember.setUpdateTime(DateUtils.getNowDate());
        bmsLeaderMember.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        return bmsLeaderMemberMapper.updateAgencyFlag(bmsLeaderMember);
    }

    /**
     * 批量删除项目负责人人员
     *将此人从人员清单管理中移除，同步解除该用户和项目负责人属下项目之间的关系。 在项目成员的“成员历史变更记录”中记录此信息
     * 
     * @param guids 需要删除的项目负责人人员ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBmsLeaderMemberByGuids(String[] guids, String leader) throws Exception {
        if(guids==null||guids.length==0||StringUtils.isBlank(leader)){
            return 0;
        }
        delProjectMembers(guids, leader);
        return bmsLeaderMemberMapper.deleteBmsLeaderMemberByGuids(guids);
    }

    /**
     * 同步删除项目成员其他信息
     * 项目负责人人员信息删除
     * @param guids
     * @param leader
     */
    private void delProjectMembers(String[] guids, String leader) throws Exception {
        List<String> userIds = bmsLeaderMemberMapper.selectUserIdByBmsProjectLeaderGuids(guids);
        if(CollectionUtils.isEmpty(userIds)){
            return;
        }
        String projectLeaderNo = sysUserService.findProjectLeaderNoByUserGuid(leader);
        List<BmsProjectMember> bmsProjectMembers = new ArrayList<>();
        if(StringUtils.isNotBlank(projectLeaderNo)){
            bmsProjectMembers = bmsProjectMemberService.selectBmsProjectMemberListByLeaderAndUserGuids(projectLeaderNo, userIds.toArray(new String[userIds.size()]));
        }
        //不为空，则存在该用户在项目负责人下项目成员中
        if(!CollectionUtils.isEmpty(bmsProjectMembers)){
            for (BmsProjectMember bmsProjectMember : bmsProjectMembers) {
                bmsProjectMemberService.deleteBmsProjectMemberByGuid(bmsProjectMember.getGuid(),"prjectList");
            }
        }
    }

    /**
     * 删除项目负责人人员信息
     * 
     * @param guid 项目负责人人员ID
     * @return 结果
     */
    @Override
    public int deleteBmsLeaderMemberByGuid(String guid, String leader) throws Exception {
        if(StringUtils.isBlank(guid)||StringUtils.isBlank(leader)){
            return 0;
        }
        delProjectMembers(new String[]{guid}, leader);
        return bmsLeaderMemberMapper.deleteBmsLeaderMemberByGuid(guid);
    }

    /**
     *  查询业务系统用户
     * 不包含在本负责人人员一览已添加用户
     * @param bmsLeaderMember
     * @return
     */
    @Override
    public List<SysUser> selectSysUserList(BmsLeaderMember bmsLeaderMember) {
        return bmsLeaderMemberMapper.selectSysUserList(bmsLeaderMember);
    }

    /**
     * 查询负责人人员并且不在项目成员中
     * @param bmsLeaderMember
     * @return
     */
    @Override
    public List<BmsLeaderMember> selectBmsLeaderMemberNoProjectMember(BmsLeaderMember bmsLeaderMember) {
        return bmsLeaderMemberMapper.selectBmsLeaderMemberNoProjectMember(bmsLeaderMember);
    }

    /**
     * 查询项目负责人人员
     * @param bmsLeaderMember
     * @return
     */
    @Override
    public List<BmsLeaderMember> selectBmsLeaderMemberLeaderList(BmsLeaderMember bmsLeaderMember) {
        return bmsLeaderMemberMapper.selectBmsLeaderMemberLeaderList(bmsLeaderMember);
    }

    /**
     * 管理员授权列表
     * @param leader
     * @return
     */
    @Override
    public List<BmsLeaderMember> selectAdminAuthorizeList(String leader) {
        return bmsLeaderMemberMapper.selectAdminAuthorizeList(leader);
    }
    
    @Override
    public List<BmsLeaderMember> selectAgencyList(String leader) {
        return bmsLeaderMemberMapper.selectAgencyList(leader);
    }

    /**
     * 是否有重复人员清单
     * @param leader
     * @param userId
     * @return
     */
    @Override
    public boolean repeatBmsLeaderMember(String leader, String userId) {
        BmsLeaderMember newBmsLeaderMember = new BmsLeaderMember();
        newBmsLeaderMember.setLeader(leader);
        newBmsLeaderMember.setUserId(userId);
        List<BmsLeaderMember> bmsLeaderMembers = this.selectBmsLeaderMemberList(newBmsLeaderMember);
        if (!CollectionUtils.isEmpty(bmsLeaderMembers)) {
            return true;
        }
        return false;
    }

	@Override
	public int checkLeaderMember(BmsLeaderMember bmsLeaderMember) {
		return bmsLeaderMemberMapper.checkLeaderMember(bmsLeaderMember);
	}

}
